Professional Web Applications Themes

Ok I am tired - PERL Beginners

> "Sharad Gupta" <Sharad.Guptanetapp.com> wrote in message >news:482A3FA0050D21419C269D13989C61131A52FElaven der-fe.eng.netapp.com... > Hi All, > > I am really tired now: > > -------------------------------------- > package Foo; > > use strict; > use LWP::UserAgent; > > > sub new { > return bless {},shift; > } > > > sub Foo::INC { > my ($self,$filename) = _; > my paths = "http:/me.com"; > my urls = map{$_ . "/" . $filename}paths; > my $ua = LWP::UserAgent->new(); > foreach my $url(urls) { > my $request = HTTP::Request->new($url); > my $response = $ua->request($request); > if($response->is_success()) { > return $response->content(); > } > } > ...

  1. #1

    Default Re: Ok I am tired

    > "Sharad Gupta" <Sharad.Guptanetapp.com> wrote in message
    >news:482A3FA0050D21419C269D13989C61131A52FElaven der-fe.eng.netapp.com...
    > Hi All,
    >
    > I am really tired now:
    >
    > --------------------------------------
    > package Foo;
    >
    > use strict;
    > use LWP::UserAgent;
    >
    >
    > sub new {
    > return bless {},shift;
    > }
    >
    >
    > sub Foo::INC {
    > my ($self,$filename) = _;
    > my paths = "http:/me.com";
    > my urls = map{$_ . "/" . $filename}paths;
    > my $ua = LWP::UserAgent->new();
    > foreach my $url(urls) {
    > my $request = HTTP::Request->new($url);
    > my $response = $ua->request($request);
    > if($response->is_success()) {
    > return $response->content();
    > }
    > }
    > return undef;
    > }
    > --------------------------------------
    >
    >
    > What i am trying to do is hook into the INC so that i can find the
    modules via http, but a simple test like:
    >
    > --------------
    > #!/usr/local/bin/perl
    >
    > BEGIN { push INC,Foo->new() }
    > use Bar;
    > -------------
    >
    > Cannot find Bar.pm.
    >
    >
    > Any ideas??.
    interesting. without you providing much background information, purpose
    (actually you did say you are trying to hook into INC but i don't really
    know what you mean by that), etc, i can only guess that you are trying to
    build, compile and include some other modules when your users say "use Foo".
    below is a toy program that hopefully will get you closer to what you need:

    package Foo;

    END{ unlink ms }

    our ms;

    sub new{
    return bless {} => shift;
    }

    sub hi{
    print "hi\n";
    }

    #--
    #-- dynamicly build and include whatever modules the users requested
    #--
    sub import{

    my $class = shift;

    for(_){

    open(NM,">$_.pm") || die $!;

    push(ms, "$_.pm");

    print NM qq/
    package $_;
    sub new{
    return {} => shift;
    }
    sub $_\_say_hi{
    print "$_ says hi\\n";
    }
    1;
    /;

    close(NM);

    require "$_.pm";
    }
    }

    1;

    __END__

    a driver:

    #!/usr/bin/perl -w
    use strict;

    #--
    #-- Foo.pm builds Bar.pm and Ber.pm and Bee.pm and use it
    #-- in fact, you can put as many module names here and Foo.pm will build it
    for you
    #--
    use Foo qw(Bar Ber Bee);

    my $f = Foo->new; $f->hi;

    #--
    #-- no need to 'use Bar' or 'use Ber' or 'use Bee' because Foo.pm does that
    for us per request
    #--
    my $bar = Bar->new; $bar->Bar_say_hi;
    my $ber = Ber->new; $ber->Ber_say_hi;
    my $bee = Bee->new; $bee->Bee_say_hi;

    __END__

    prints:

    hi
    Bar says hi
    Ber says hi
    Bee says hi

    in fact, if you look around, you wouldn't even find Bar.pm, Ber.pm or Bee.pm
    anywhere because they only exist during your program runs and are deleted
    automatically when your program ends. the user never know the existance of
    them but are available for use when they need it! a pretty neat idea but
    kind of dangerous (especially if you are downloading the modules from the
    Net). you can use this technique (if you really want to do that and know
    exactly what you are doing) to build arbitary complex class on the fly. the
    idea is very simply though. again, without knowing what you are really
    trying to accomplish, i can't recommand anything else.

    david


    David Guest

  2. #2

    Default RE: Ok I am tired

    Ur examples are cool. I never knew i can do that.

    But my problem was:

    I was trying to port some of my scripts to Windows.
    I know i can get all of the modules which my already written scripts would be using via http. And i did'nt wanted to map the drive on *all* the windows machines and add it to to INC before using them.

    So, i thought may be i can put something in INC so that I never care where the modules/scripts are coming from.

    Now with my implementation, my idea is i would ask my sysadmin to have a system wide:
    PERL5OPTS = -MFoo

    variable set.

    So that whenever anybody says :

    #<path to perl>

    use Bar;

    They get a copy of Bar w/o copying it locally.



    Hope i stated my problem well.


    Now another question:

    In ur examples u r are unlinking the files in the END block, and i was unlinking them in the DESTROY method. Any differences??

    -Sharad

    -----Original Message-----
    From: david [mailto:dzhuolooksmart.net]
    Sent: Saturday, August 09, 2003 12:27 AM
    To: [email]beginnersperl.org[/email]
    Subject: Re: Ok I am tired

    > "Sharad Gupta" <Sharad.Guptanetapp.com> wrote in message
    >news:482A3FA0050D21419C269D13989C61131A52FElaven der-fe.eng.netapp.com....
    > Hi All,
    >
    > I am really tired now:
    >
    > --------------------------------------
    > package Foo;
    >
    > use strict;
    > use LWP::UserAgent;
    >
    >
    > sub new {
    > return bless {},shift;
    > }
    >
    >
    > sub Foo::INC {
    > my ($self,$filename) = _;
    > my paths = "http:/me.com";
    > my urls = map{$_ . "/" . $filename}paths;
    > my $ua = LWP::UserAgent->new();
    > foreach my $url(urls) {
    > my $request = HTTP::Request->new($url);
    > my $response = $ua->request($request);
    > if($response->is_success()) {
    > return $response->content();
    > }
    > }
    > return undef;
    > }
    > --------------------------------------
    >
    >
    > What i am trying to do is hook into the INC so that i can find the
    modules via http, but a simple test like:
    >
    > --------------
    > #!/usr/local/bin/perl
    >
    > BEGIN { push INC,Foo->new() }
    > use Bar;
    > -------------
    >
    > Cannot find Bar.pm.
    >
    >
    > Any ideas??.
    interesting. without you providing much background information, purpose
    (actually you did say you are trying to hook into INC but i don't really
    know what you mean by that), etc, i can only guess that you are trying to
    build, compile and include some other modules when your users say "use Foo".
    below is a toy program that hopefully will get you closer to what you need:

    package Foo;

    END{ unlink ms }

    our ms;

    sub new{
    return bless {} => shift;
    }

    sub hi{
    print "hi\n";
    }

    #--
    #-- dynamicly build and include whatever modules the users requested
    #--
    sub import{

    my $class = shift;

    for(_){

    open(NM,">$_.pm") || die $!;

    push(ms, "$_.pm");

    print NM qq/
    package $_;
    sub new{
    return {} => shift;
    }
    sub $_\_say_hi{
    print "$_ says hi\\n";
    }
    1;
    /;

    close(NM);

    require "$_.pm";
    }
    }

    1;

    __END__

    a driver:

    #!/usr/bin/perl -w
    use strict;

    #--
    #-- Foo.pm builds Bar.pm and Ber.pm and Bee.pm and use it
    #-- in fact, you can put as many module names here and Foo.pm will build it
    for you
    #--
    use Foo qw(Bar Ber Bee);

    my $f = Foo->new; $f->hi;

    #--
    #-- no need to 'use Bar' or 'use Ber' or 'use Bee' because Foo.pm does that
    for us per request
    #--
    my $bar = Bar->new; $bar->Bar_say_hi;
    my $ber = Ber->new; $ber->Ber_say_hi;
    my $bee = Bee->new; $bee->Bee_say_hi;

    __END__

    prints:

    hi
    Bar says hi
    Ber says hi
    Bee says hi

    in fact, if you look around, you wouldn't even find Bar.pm, Ber.pm or Bee.pm
    anywhere because they only exist during your program runs and are deleted
    automatically when your program ends. the user never know the existance of
    them but are available for use when they need it! a pretty neat idea but
    kind of dangerous (especially if you are downloading the modules from the
    Net). you can use this technique (if you really want to do that and know
    exactly what you are doing) to build arbitary complex class on the fly. the
    idea is very simply though. again, without knowing what you are really
    trying to accomplish, i can't recommand anything else.

    david



    --
    To unsubscribe, e-mail: [email]beginners-unsubscribeperl.org[/email]
    For additional commands, e-mail: [email]beginners-helpperl.org[/email]


    Sharad Gupta Guest

  3. #3

    Default Re: Ok I am tired

    On Fri, Aug 08, 2003 at 10:49:39PM -0700, Gupta, Sharad wrote:
    > sub Foo::INC {
    > my ($self,$filename) = _;
    > my paths = "http:/me.com";
    > my urls = map{$_ . "/" . $filename}paths;
    > my $ua = LWP::UserAgent->new();
    > foreach my $url(urls) {
    > my $request = HTTP::Request->new($url);
    > my $response = $ua->request($request);
    > if($response->is_success()) {
    > return $response->content();
    You're supposed to return a filehandle:

    open my $fh, '<', \($response->content);
    return $fh;
    > }
    > }
    > return undef;
    > }
    I'd also suggest caching the downloaded modules and letting
    the constructor take a list of base urls.

    And by the way, does anybody know offhand why the subroutine
    name needs to be fully-qualified in the declaration? I haven't
    really looked at this feature, but it seems like an strange
    requirement.

    --
    Steve
    Steve Grazzini Guest

  4. #4

    Default RE: Ok I am tired

    >> You're supposed to return a filehandle:

    Yep. But i am using the temp files for doing that. And i would love to get rid of the temp files altogether.

    open my $fh, '<', \($response->content);

    Seems like we are opening a filehandle to a string. Very difficult to convince my sysadmin to get to 5.8 soon. Any other suggestions to get rid of those temp files. I hate them.

    >> I'd also suggest caching the downloaded modules
    Give me a little of your magical sight -:)

    >> letting the constructor take a list of base urls.
    Yes, thats what i would be doing in production.

    >>And by the way, does anybody know offhand why the subroutine
    >>name needs to be fully-qualified in the declaration?
    Require says:

    If the hook is an object, it must provide an INC method, that will be called as above, the first parameter being the object itself. (Note that you must fully qualify the sub's name, as it is always forced into package main.)


    Thanx,
    -Sharad

    -----Original Message-----
    From: Steve Grazzini [mailto:grazzpobox.com]
    Sent: Saturday, August 09, 2003 1:51 PM
    To: Gupta, Sharad
    Cc: [email]beginnersperl.org[/email]
    Subject: Re: Ok I am tired


    On Fri, Aug 08, 2003 at 10:49:39PM -0700, Gupta, Sharad wrote:
    > sub Foo::INC {
    > my ($self,$filename) = _;
    > my paths = "http:/me.com";
    > my urls = map{$_ . "/" . $filename}paths;
    > my $ua = LWP::UserAgent->new();
    > foreach my $url(urls) {
    > my $request = HTTP::Request->new($url);
    > my $response = $ua->request($request);
    > if($response->is_success()) {
    > return $response->content();
    You're supposed to return a filehandle:

    open my $fh, '<', \($response->content);
    return $fh;
    > }
    > }
    > return undef;
    > }
    I'd also suggest caching the downloaded modules and letting
    the constructor take a list of base urls.

    And by the way, does anybody know offhand why the subroutine
    name needs to be fully-qualified in the declaration? I haven't
    really looked at this feature, but it seems like an strange
    requirement.

    --
    Steve

    Sharad Gupta Guest

  5. #5

    Default Re: Ok I am tired

    On Sat, Aug 09, 2003 at 08:57:54PM -0700, Gupta, Sharad wrote:
    >> You're supposed to return a filehandle:
    >
    > Yep. But i am using the temp files for doing that. And i
    > would love to get rid of the temp files altogether.
    >
    >> open my $fh, '<', \($response->content);
    >
    > Seems like we are opening a filehandle to a string. Very
    > difficult to convince my sysadmin to get to 5.8 soon. Any
    > other suggestions to get rid of those temp files. I hate
    > them.
    IO::Stringy (non-core module) was the old way to do it...
    >> I'd also suggest caching the downloaded modules
    >
    > Give me a little of your magical sight -:)
    It's not secure, but in a safe environment you could write
    $res->content to some cache directory. Then before downloading
    anything, just check the cache.
    >> And by the way, does anybody know offhand why the subroutine
    >> name needs to be fully-qualified in the declaration?
    >
    > Require says:
    >
    > Note that you must fully qualify the sub's name, as it is
    > always forced into package main.
    Yeah, that seems strange.

    I suppose that *INC is "special", for the sake of %INC and
    INC... But I didn't expect it to force the subroutine
    declaration into main:: as well.

    --
    Steve
    Steve Grazzini Guest

Similar Threads

  1. tired of counting.... UDP socket bug
    By Ray Cassick \(Home\) in forum ASP.NET Security
    Replies: 5
    Last Post: January 19th, 02:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139