Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Default import function

by choroba (Cardinal)
on Oct 24, 2016 at 06:55 UTC ( [id://1174573]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Default import function
in thread Default import function

I haven't checked the source, but it seems so, don't you think? The created sub seems empty:
~ $ perl -lwe '{package My; sub x {}} $INC{"My.pm"}=1; eval "use My"; +use Data::Dumper; $Data::Dumper::Deparse = 1; print Dumper \&$_ for v +alues %{"My::"}' $VAR1 = sub { package My; }; $VAR1 = sub ;;

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^4: Default import function
by Anonymous Monk on Oct 24, 2016 at 13:08 UTC

    That makes sense, at least in retrospect. As was mentioned earlier, use() is equivalent to require() followed by import(). The minimum implementation is probably to have class UNIVERSAL, which all Perl classes inherit from, have an empty import(), which in fact it does.

    So, to go back to the original question, it appears the most useful answer is that your Perl class in fact gets an import() method from somewhere, but the default one, inherited from UNIVERSAL, does nothing, so presumably they felt no need to document it. It's not documented under UNIVERSAL either.

      It doesn't work as you think!

      An import method is defined in UNIVERSAL.pm, but that file is not loaded unless it is explicitly requested as use UNIVERSAL; which is very rare practice.

      So, how does Perl handle the import method in modules not defining it?

      It is a special case:

      /* excerpt from gv.c in the perl source code */ if (!gv) { if (strEQ(name,"import") || strEQ(name,"unimport")) gv = MUTABLE_GV(&PL_sv_yes); else if (autoload) gv = gv_autoload_pvn( ostash, name, nend - name, GV_AUTOLOAD_ISMETHOD|flags ); if (!gv && do_croak) { /* Right now this is exclusively for the benefit of S_method_c +ommon in pp_hot.c */
      when in a method call the requested method can not be resolved, perl checks if it is actually import (or unimport) and in that case, the call is just ignored.
      The empty import doesn't seem to be the one coming from UNIVERSAL:
      # UNIVERSAL should not contain any extra subs/methods beyond those # that it exists to define. The existence of import() below is a histo +rical # accident that can't be fixed without breaking code. # Make sure that even though the import method is called, it doesn't d +o # anything unless called on UNIVERSAL. sub import { return unless $_[0] eq __PACKAGE__; return unless @_ > 1; require Carp; Carp::croak("UNIVERSAL does not export anything"); }

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1174573]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-23 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found