Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

What does this code do?

by John M. Dlugosz (Monsignor)
on Oct 03, 2002 at 20:29 UTC ( [id://202637]=perlquestion: print w/replies, xml ) Need Help??

John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

In Exporter.pm,
if ($Verbose or $Debug or grep {/\W/ or $args and not exists $exports{$_} or @fail and $_ eq $fail[0] or (@{"$pkg\::EXPORT_OK"} and $_ eq ${"$pkg\::EXPORT_OK"}[0])} @_) { return export $pkg, $callpkg, ($args ? @_ : ()); } #local $SIG{__WARN__} = sub {require Carp; goto &Carp::carp}; local $SIG{__WARN__} = sub {require Carp; local $Carp::CarpLevel = 1; &Carp::carp}; foreach $sym (@_) { # shortcut for the common case of no type character *{"$callpkg\::$sym"} = \&{"$pkg\::$sym"}; }
It appears that the concept is to call export in the general case, and do an inline symbol assignment if all special conditions are met.

But just what does that condition say? I understand setting $Verbose or $Debug, and see that the rest is inside a grep to test each argument against some conditions.

But, what is the significance of the first element of @EXPORT_OK or the first element of @fail? Basically, if one of the arguments is the first entry in @EXPORT_OK, then the general case is performed.

So what's special about the first OK item?

—John

Replies are listed 'Best First'.
Re: What does this code do?
by jwest (Friar) on Oct 03, 2002 at 20:47 UTC
    I don't think this has anything to do with the position of the item in @EXPORT_OK or @fail.

    What the author of the code seems to be interested in is if any of the members of the @_ list appear in @EXPORT_OK or @fail. If one cycles through every memeber of @_ one will appear as the first element. Likewise, if you like, you could check against $EXPORT_OK[-1] and $fail[-1] and get the same result.

    Hope this helps!

    --jwest



    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
    
      Suppose @EXPORT_OK= qw/foo bar baz/; And the import list is (bar) alone. Just because the whole import list is scanned doesn't mean that if any element matches something in @EXPORT_OK then all elements in @EXPORT_OK are matched!

      In this example, nothing in @_ matches $EXPORT_OK[0] or $EXPORT_OK[-1] for that matter. But something does match something in @EXPORT_OK.

      So... I still don't get it.

        You're absolutely right. I had, apparently mistakenly, assumed that @_ was a superset of the contents of both @EXPORT_OK and @fail, instead of a subset of either.

        Lacking the context of how @_ is being defined, it was an easy assumption to make. Without visibility into the calling code, I'm reticent to hazard another guess.

        I suppose it's not oustide the scope of reason that the code may not reflect the author's intent. In a module this pervasive, however, it's not likely that this is the case.

        --jwest

        -><- -><- -><- -><- -><-
        All things are Perfect
            To every last Flaw
            And bound in accord
                 With Eris's Law
         - HBT; The Book of Advice, 1:7
        
Re: What does this code do?
by blokhead (Monsignor) on Oct 04, 2002 at 04:54 UTC
    Earlier in the same sub in Exporter.pm, you'll see this line:
    return export $pkg, $callpkg, @_ if $Verbose or $Debug or @fail > 1;
    So it won't make it to your snippet in question if @fail > 1. So $fail[0] seems reasonable. As for the significance of $EXPORT_OK[0], I'm as lost as you are. I don't see anywhere where it's enforced to have only one element, as with @fail. What a curious little module, Exporter.pm.

    blokhead

Re: What does this code do?
by VSarkiss (Monsignor) on Oct 03, 2002 at 20:55 UTC

    I'm not sure, but I think what's happening is that the code wants to check whether any of the arguments to import occur in @EXPORT_OK or @EXPORT_FAIL. In either of those cases, it has to call the "heavy" version, rather than the simple code below it. Now, if any of the arguments match an entry in those arrays, something has to match the first element (recall it's grep'ing through @_), so it may as well check there first. In other words, any other element of @EXPORT_OK or @EXPORT_FAIL would have served equally well.

    Of course, I could be wrong....

      Now, if any of the arguments match an entry in those arrays, something has to match the first element (recall it's grep'ing through @_), so it may as well check there first. In other words, any other element of @EXPORT_OK or @EXPORT_FAIL would have served equally well.

      I don't follow that logic. Just because something in @EXPORT_OK is matched doesn't imply that everything in @EXPORT_OK is matched.

      See my example in my reply to jwest.

      —John

Re: What does this code do?
by shotgunefx (Parson) on Oct 04, 2002 at 09:31 UTC
    Might the first element reference be for specialized exporting?

    Specialised Import Lists
    If the first entry in an import list begins with !, : or / then the list is treated as a series of specifications which either add to or delete from the list of names to import. They are processed left to right. Specifications are in the form:

    [!]name This name only
    [!]:DEFAULT All names in @EXPORT
    [!]:tag All names in $EXPORT_TAGS{tag} anonymous list
    [!]/pattern/ All names in @EXPORT and @EXPORT_OK
    which match A leading ! indicates that matching names should be deleted from the list of names to import. If the first specification is a deletion it is


    -Lee

    "To be civilized is to deny one's nature."
      The fancy stuff (negation, tags, patterns) only work if the first item is such. Something like use Module qw( foo :tag ); will not work, since the code will skip expanding the fancy stuff if the first argument doesn't trigger it.

      But that has nothing to do with the conjectured significance of the first item in @EXPORT_OK.

        Just a guess. Don't have the time or inclination to dig deep.

        -Lee

        "To be civilized is to deny one's nature."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://202637]
Approved by dws
Front-paged by jarich
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-03-29 02:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found