Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

unknown code

by MurciaNew (Novice)
on Feb 14, 2018 at 07:55 UTC ( #1209108=perlquestion: print w/replies, xml ) Need Help??
MurciaNew has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am trying to learn more nice perl hacks, so I looked to modules in my perl lib (here File::Find). I found some constructs that I never saw before. Maybe someone can explain me these constructs or code snippets or the idea behind:

1) eval { eval "# $nada" }; # eval eval? evil!? ;-) 2) (-d _) # the underline? 3) 1 while $abs_name =~ s!/<regexp>/!; 4) local *_ = \my $a; 5) $SLnkSeen{$DEV, $INO}++; # %SLnkSeen 6) *name # for what the star? uses?

Thanks.

Greetings && ohm Guido

Replies are listed 'Best First'.
Re: unknown code
by ikegami (Pope) on Feb 14, 2018 at 08:16 UTC

    1. Looks safe because it looks like a comment is being evaluated, but what if $nada contained a newline? If text is being inserted in to code without first being converted into code, you have a code injection bug.

    2. [See (6) first] _ refers to *_, the glob (symbol table entry) that contains $_, @_, etc. Here, it's used as a file handle, presumably populated by an earlier stat or -X operator.

    3. s/// returns true if it matched, so this code repeatedly performs the substitution until it doesn't match. This pattern is usually buggy. Replacing the whole with a single s///eg will solve these bugs.

    4. [See (6) first] Creates a lexical variable named $a and aliases $_ to it until the end of the current lexical scope. Note that creating a lexical named $a will cause problems for sort and similar functions.

    5. $h{$i, $j, ...} is equivalent to $h{join($;, $i, $j, ...)}.

    6. *name is a typeglob, or just glob for short. Globs are used as the entries of the symbol table, so a glob is a struct with a slot for each type of variable (scalar, array, hash, glob, code) and few others (file handle, directory handle, format). I could be missing some. One might say it's a bunch of variables of different types, perhaps even a glob of types... Globs are weird because they are both a type of variable (e.g. *foo) and a type of scalar (e.g. $foo = *foo;).

    Update: Added missing answer for (6).

Re: unknown code
by karlgoethebier (Monsignor) on Feb 14, 2018 at 10:22 UTC

    1): Aha - it's a fallback. Full code from File::Find:

    # check whether or not a scalar variable is tainted # (code straight from the Camel, 3rd ed., page 561) sub is_tainted_pp { my $arg = shift; my $nada = substr( $arg, 0, 0 ); # zero-length local $@; eval { eval "# $nada" }; return length($@) != 0; } # We need a function that checks if a scalar is tainted. # Either use the # Scalar::Util module's tainted() function # or our (slower) pure Perl # fallback is_tainted_pp() { local $@; eval { require Scalar::Util }; *is_tainted = $@ ? \&is_tainted_pp : \&Scalar::Util::tainted; }

    And i'm banging my head against a wall to understand it. This code mucked my day ;-)

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: unknown code -- typeglob
by Discipulus (Monsignor) on Feb 14, 2018 at 09:22 UTC
    6) *name ...

    This is a Perl typeglob: see Typeglobs on perldata

    Typeglobs are the internal data of which the symbol table is filled. Hic sunt leones .. here black magic begins..

    See also many links on my homenode

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: unknown code
by rizzo (Scribe) on Feb 14, 2018 at 09:21 UTC

    6.) Typeglob

Re: unknown code (ppi_dumper knows everything)
by Anonymous Monk on Feb 14, 2018 at 23:33 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1209108]
Approved by ikegami
Front-paged by haukex
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2018-04-19 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?