Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the Perl asterisk sigil do

Tags:

perl

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.

like image 535
user3276159 Avatar asked Mar 03 '16 18:03

user3276159


2 Answers

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;).

like image 97
ikegami Avatar answered Nov 13 '22 07:11

ikegami


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. $.

like image 32
Oleg V. Volkov Avatar answered Nov 13 '22 08:11

Oleg V. Volkov