I am working on updating some very old Perl code (not my code) and I came across the following in a subroutine:
sub DoEvent {
local(*eventWEH, $foreachFunc, @args) = @_;
$ret = do $foreachFunc(*eventWEH, @args);
}
This seemed to work fine in Perl 5.6.1 but no longer works in Perl 5.22 which gives a syntax error just after the do $foreachFun(
What is the purpose of the asterisk in front of the variable *eventWEH?
If this were "C" I would suggest some sort of pointer. Any suggestions?
ETA: This sub gets called with lots of different types for the first parameter. For example:
&DoEvent($abc, "DoAbc", @args);
and
&DoEvent(@xyz, "DoXyz", @args);
So as per the answers given, it looks like the *eventWEH can take on a different data type to match whatever parameter is passed to the sub.
The syntax error has nothing to do with the globs (the symbols with the asterisk sigil). The syntax error is due to the removal of do $func(LIST)
in 5.20. Replace
$ret = do $foreachFunc(*eventWEH, @args);
with
$ret = $foreachFunc->(*eventWEH, @args);
As for your literal question, *foo
is a typeglob, or glob for short. A glob is a proxy of an instance of the following C structure:
struct gp {
SV * gp_sv; /* scalar value */
struct io * gp_io; /* filehandle value */
CV * gp_cv; /* subroutine value */
U32 gp_cvgen; /* generational validity of cached gp_cv */
U32 gp_refcnt; /* how many globs point to this? */
HV * gp_hv; /* hash value */
AV * gp_av; /* array value */
CV * gp_form; /* format value */
GV * gp_egv; /* effective gv, if *glob */
PERL_BITFIELD32 gp_line:31; /* line first declared at (for -w) */
PERL_BITFIELD32 gp_flags:1;
HEK * gp_file_hek; /* file first declared in (for -w) */
};
Globs are primarily used internally, though some file handles are primarily accessed via named globs (*STDIN
, *STDOUT
and *STDERR
), and they are used to create aliases (*foo = \&bar;
).
It is namespace symbol sigil that contains all other types. In older Perl code it is usually used to work with file handles. Modern Perl should use simple scalars - i.e. $
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With