in reply to UrlLib and urllib2 python to perl

Why?

 [UrlLib python]  [UrlLib] UrlLib python UrlLib -> Perl equivalent to Python's urllib module?

Replies are listed 'Best First'.
Re^2: UrlLib and urllib2 python to perl
by Anonymous Monk on Aug 13, 2013 at 09:21 UTC
    Looks like Web::Magic cant do https...

      Looks like Web::Magic cant do https...

      Sure, if you look with your eyes closed

        Pardon my ignorance.Maybe it does.Couldnt find examples. Any Idea why this error:
        use 5.010; use strict; use utf8::all; use Web::Magic my $search = web <https://10.219.136.2/auth1.html>; print $search;
        Compile:
        H:\Work\perl\latest>perl webmagic.pl syntax error at webmagic.pl line 7, near "https:"
Re^2: UrlLib and urllib2 python to perl
by sidsinha (Acolyte) on Aug 13, 2013 at 03:22 UTC

    Very good question.

    Indeed I use perl for lot of other things. So I want to use perl completely in my environment rather than have one script using perl n another for python.

    Also after this am planning to use CGI so perl will help me. Thanks
      ok Web::Magic looks great. is there an equivalent function for hashlib? Couldnt find any
        Digest::MD5 should be a suitable replacement here:
        % python -c'import hashlib;print hashlib.md5("blah").hexdigest()' 6f1ed002ab5595859014ebf0951522d9
        % perl -MDigest::MD5 -E'say Digest::MD5::md5_hex("blah")' 6f1ed002ab5595859014ebf0951522d9

        ok Web::Magic looks great. is there an equivalent function for hashlib? Couldnt find any

        I don't know what that is, or what that does, but if you go to MetaCPAN or CPAN and enter the name of the algorithm (or even "hash") you'll find something

Re^2: UrlLib and urllib2 python to perl
by Anonymous Monk on Aug 13, 2013 at 07:52 UTC
    How could the below part be done in perl? the function of re.search
    id_obj = re.search('name=\"id\" value=\"[A-Za-z0-9]*\" size=',respons +e_data).group(0) param1_obj = re.search('NAME=\"param1\" VALUE=\"[A-Za-z0-9]*\"',re +sponse_data).group(0) param2_obj = re.search('NAME=\"param2\" VALUE=\"[A-Za-z0-9]*\"',re +sponse_data).group(0) sessId_obj = re.search('NAME=\"sessId\" VALUE=\"[A-Za-z0-9]*\"',re +sponse_data).group(0) param1 = param1_obj.split('\"')[3] param2 = param2_obj.split('\"')[3] id = id_obj.split('\"')[3] sessId= sessId_obj.split('\"')[3]

        To help with the first question that our fellow Anonymonk named: it tries to match the regex (first parameter) in the string (second parameter). .group(0) returns the matched string.

        Differences between Python and Perl:

        • Python's regexes are written as strings, so there are different escape rules. in a perl regex, no need to escape the quotes, the first regex could simply be written as
          /name="id" value="[A-Za-z0-9]*" size=/
        • to receive the complete matched string, in perl you would use $& or modify the regex with the /p flag and then use ${^MATCH}.

        The python code then splits the found match to extract the part after value=. I would use a group instead, even in Python. I would write

        for (qw(id param1 param2 sessId) { $response_data =~ /name="$_" value="([A-Za-z0-9]*)" size=/; $v[$_] = $1; }
        Although afterwards the values aren't in variables $id, $param1, etc but in $v["id"] etc, however this might come in handy anyway, because most probably the things you will be doing to each of them will be similiar, and you can do them by employing similiar loops.