Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Use + versus Require

by jeffthewookiee (Sexton)
on Dec 14, 2007 at 20:49 UTC ( [id://657112]=perlquestion: print w/replies, xml ) Need Help??

jeffthewookiee has asked for the wisdom of the Perl Monks concerning the following question:

Using SOAP::Lite, I've come upon some interesting syntax:
use SOAP::Lite +autodispatch => proxy => http://soap.soapserver.com', on_fault => \&handle_error;
What significance does the + play in how this code is compiled? Is it possible to convert this code to a run-time version using 'require', or is this particular syntax only available with use?

Replies are listed 'Best First'.
Re: Use + versus Require
by Corion (Patriarch) on Dec 14, 2007 at 21:15 UTC

    See use. use Some::Module LIST is essentially

    BEGIN { require Some::Module; Some::Module->import( LIST ); }

    So just adapt that to your needs. A likely literal translation gives me:

    require SOAP::Lite; SOAP::Lite->import( +autodispatch => 'proxy' => http://soap.soapserver.com', # there +is a typo on this line, in the original post on_fault => \&handle_error; )

    The "fat comma operator", => simply stringifies its left-hand operand. So this is equivalent to

    require SOAP::Lite; SOAP::Lite->import( '+autodispatch' => 'proxy' => http://soap.soapserver.com', # there +is a typo on this line, in the original post 'on_fault' => \&handle_error;

      No, the + is just the unary + operator applied to the bareword which is just the bareword itself; it doesn't make it part of the argument.

      $ perl -le 'BEGIN{package Foo; sub import { print join( "\n", @_ ) }; +$INC{"Foo.pm"}=1; } use Foo +bar => "baz", -quux => "jinkies";' Foo bar baz -quux jinkies

      I think SOAP::Lite uses the + as a visual cue to distinguish it from options which are prefixed by a - to indicate they're disabled (and the fat comma special cases - so that -foo => "blah" does give "-foo", "blah" even though "-" isn't a \w character).

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Re: Use + versus Require
by webfiend (Vicar) on Dec 14, 2007 at 23:07 UTC

    perlop mentions that unary + has no effect, even on strings. SOAP::Lite::import doesn't seem to care about plus or minus symbols in that initial import list item.

    Near as I can tell, the '+' is there because the module author thought it was pretty.

    There may be more to it than that, though. I have worked at places where they insisted on prefixing hash keys with a '+' symbol. I never figured out why, and it was a habit I stayed away from. There's enough of a noise problem with my code as it is.

Re: Use + versus Require
by gam3 (Curate) on Dec 15, 2007 at 16:41 UTC
    Here is a test file.
    package MyTest; use Data::Dumper; sub import { warn Dumper \@_; } package main; BEGIN { # code to keep use and require for complaining # that there is no MyTest.pm file $INC{'MyTest.pm'} = 'testfile'; } sub handle_error{ die; } use MyTest +autodispatch => proxy => 'use'; BEGIN { require MyTest; MyTest->import( +autodispatch => proxy => 'require', ); } MyTest->import( '+autodispatch' => proxy => 'require as string', );
    The output
    $VAR1 = [ 'MyTest', 'autodispatch', 'proxy', 'use' ]; $VAR1 = [ 'MyTest', 'autodispatch', 'proxy', 'require' ]; $VAR1 = [ 'MyTest', '+autodispatch', 'proxy', 'require as string' ];
    The only really interesting thing I see is that
        +autodispatch =>
    
    and
       '+autodispatch' =>
    
    are not the same.
    -- gam3
    A picture is worth a thousand words, but takes 200K.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 11:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found