Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Help Changing use into require/import

by computergeek (Novice)
on Mar 07, 2018 at 23:08 UTC ( [id://1210488]=note: print w/replies, xml ) Need Help??


in reply to Re: Help Changing use into require/import
in thread Help Changing use into require/import

I tried your code and get this error: Can't locate object method "new" via package "Capture::Tiny" at ./foo.pl line 40. I am not sure if this means I am dead in the water or if there is another way. When I was doing research on import it appears the module writer is the one who implements and it is not a generic perl feature.

  • Comment on Re^2: Help Changing use into require/import

Replies are listed 'Best First'.
Re^3: Help Changing use into require/import
by syphilis (Archbishop) on Mar 07, 2018 at 23:28 UTC
    Can't locate object method "new" via package "Capture::Tiny" at ./foo.pl line 40

    Capture::Tiny doesn't have a new() method, and Capture::Tiny->new() will therefore fail irrespective of how you loaded the module.

    Cheers,
    Rob

      So is there a way to load it dynamically?

        Just use require Capture::Tiny at runtime, like you already did in your code.

        The thing you are missing is that Perl doesn't know that capture should become a function. You can either try to predeclare capture as subroutine to tell that you expect a capture subroutine:

        sub capture(&@); ... require Capture::Tiny; Capture::Tiny->import('capture'); capture { } ...;

        Or go the more explicit route of calling capture in a way that doesn't need parser gymnastics:

        capture( sub { ... }, ... );
Re^3: Help Changing use into require/import
by VinsWorldcom (Prior) on Mar 07, 2018 at 23:32 UTC

    That's just my quick example. There is no new() constructor in Capture::Tiny, I was just throwing down a quick template and overlooked that. That'd be where you use the capture() sub you imported.

      I have read a "use" does a require and import but when I try to do these explicitly it just does not seem to work. You say that capture tiny has no new constructor and I do not know how to translate that to what needs to be done to load the module dynamically. I have tried to move on to ipc::run but am having similar difficulties so I for sure do not have the knowledge to do what needs to be done to load these dynamically or they cannot be loaded dynamically and again if they cannot, I don't know how to tell for sure that it is an impossible task which takes me back to the original question of I am looking to learn generically how to look at a module and tell what is required for a module and adapt a require/import combo that will work based on the way the module was written. I am in essence asking not to give me the fish although that would be appreciated but also teach me to fish so I can do this from now on.

        > "use" does a require and import

        in a BEGIN block. That's important, because it can change how the rest of the code is parsed. For OO modules, that's usually not an issue, but if the module introduces keywords with fancy syntax (like capture { ... }), you need to predeclare these with the appropriate prototypes, otherwise the parser can't parse them; or you need to call them without the special syntax (capture(sub { ... });). Read perlsub to learn about compile time and prototypes.

        ($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://1210488]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found