Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Aliasing Module->method

by gerases (Sexton)
on Feb 06, 2016 at 18:54 UTC ( #1154574=perlquestion: print w/replies, xml ) Need Help??

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

Hi, It seems that Time::Piece module doesn't export strptime. So the only way to use it is to do:
Time::Piece->strptime
Which is too long. Can I alias some "x" to Time::Piece->strptime? I tried the things offered here: http://docstore.mik.ua/orelly/perl/cookbook/ch10_15.htm but that doesn't seem to work. Thanks for any ideas!

Replies are listed 'Best First'.
Re: Aliasing Module->method
by stevieb (Canon) on Feb 06, 2016 at 19:23 UTC

    First off, the confusion later down the road by using my code below isn't remotely close to being worth saving a few keystrokes, but to answer your question, you can use a typeglob, and wrap the call within an anonymous sub. Note how I pass in the parameters (@_) through the anonysub and directly into the original call:

    use warnings; use strict; use Time::Piece; *st = sub { Time::Piece->strptime(@_) }; st("Sunday 3rd Nov, 1943", "%A %drd %b, %Y",);
      Ahhh... Cool!! Thank you for such a quick response.
Re: Aliasing Module->method
by LanX (Sage) on Feb 06, 2016 at 20:55 UTC
    > Time::Piece module doesn't export strptime.

    because it has an object oriented interface. IOW it's a class not a simple procedural module.

    (update: see Perl_module#Object-oriented_example )

    > Can I alias some "x" to Time::Piece->strptime?

    Aliasing a method can only be done by another method ..

    (I don't get the point?!)

    but you are free to do

    sub x { Time::Piece->strptime(@_) }

    be aware that the returned value is still an object.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Yeah, I wanted to alias the method call to a symbol within my program so I didn't have to type that much. So if the original is Time::Piece->strptime ... I wanted it to become: x->strptime. But on second thought, I of course could have created an object for Time::Piece and called strptime on the object. Didn't occur to me at the time I was writing it. I thought strptime was a CLASS method. All is good, thank you for making me think!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2022-07-03 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?