Re: Perl JSON

2008-12-14 Thread Brian J. Miller

AtiqulRE wrote:

Hi,

New to Perl. Need help to write server side using Perl that returns
JSON data.

Please include detail examples. e.g. how to take a hash and convert to
JSON or array of hashes and convert them into JSON, etc.

Thanks much.

-atiqul




Have you checked CPAN?

http://search.cpan.org/search?query=JSON&mode=all

--
Brian J. Miller
End Point Corp.
http://www.endpoint.com/
br...@endpoint.com

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Perl JSON

2008-12-14 Thread AtiqulRE
Hi,

New to Perl. Need help to write server side using Perl that returns
JSON data.

Please include detail examples. e.g. how to take a hash and convert to
JSON or array of hashes and convert them into JSON, etc.

Thanks much.

-atiqul


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: counter program by using closure

2008-12-14 Thread Richard

John W. Krahn wrote:

Richard wrote:

John W. Krahn wrote:


You want something more like this:

sub counter {
my $count;
my $clear = `clear`;
my $counting = <<'EOF';
%s
|   Counting...|
|   %2d |
|  |

EOF
return sub { local $| = 1; printf $counting, $clear, ++$count }
}

my $yeah = counter();
for ( 1 .. 35 ) {
sleep 1;
$yeah->();
}


this is interesting and this also works well.
My question is, how does perl know in this instance that %2d is 
refering to $count.. is it because $clear contains none numeric value 
or because $count contains numeric value?


$counting contains the format string for printf() and the first 
argument $clear is substituted for '%s' in $counting and the second 
argument ++$count is substituted for '%2d' in $counting.  They are 
substituted in the same order as they appear.





John
ah.. did not see the %s... and now that makes perfect sense.. thank you 
very much!!!


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: counter program by using closure

2008-12-14 Thread John W. Krahn

Richard wrote:

John W. Krahn wrote:


You want something more like this:

sub counter {
my $count;
my $clear = `clear`;
my $counting = <<'EOF';
%s
|   Counting...|
|   %2d |
|  |

EOF
return sub { local $| = 1; printf $counting, $clear, ++$count }
}

my $yeah = counter();
for ( 1 .. 35 ) {
sleep 1;
$yeah->();
}


this is interesting and this also works well.
My question is, how does perl know in this instance that %2d is refering 
to $count.. is it because $clear contains none numeric value or because 
$count contains numeric value?


$counting contains the format string for printf() and the first argument 
$clear is substituted for '%s' in $counting and the second argument 
++$count is substituted for '%2d' in $counting.  They are substituted in 
the same order as they appear.





John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: counter program by using closure

2008-12-14 Thread Richard

John W. Krahn wrote:

Richard wrote:


wanted to draw a box that's counting up for certain time.
Thought I could use the counter but it just displays the box not the 
number..


can anyone point it out for me?

thank you.

use warnings;
use strict;

sub counter {
  my $count;
  my $counting = <|   
Counting... 
|
|   
++$count;   
|
|
  |


EOF

  return sub { print $counting }
}

my $real_count;
my $yeah = counter();
while ($real_count < '35') {
 ++$real_count;
 system('clear');
 $yeah->();
}


You want something more like this:

sub counter {
my $count;
my $clear = `clear`;
my $counting = <<'EOF';
%s
|   Counting...|
|   %2d |
|  |

EOF
return sub { local $| = 1; printf $counting, $clear, ++$count }
}

my $yeah = counter();
for ( 1 .. 35 ) {
sleep 1;
$yeah->();
}



John

this is interesting and this also works well.
My question is, how does perl know in this instance that %2d is refering 
to $count.. is it because $clear contains none numeric value or because 
$count contains numeric value?




--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Regex pattern match in perl

2008-12-14 Thread John W. Krahn

explor wrote:

Hi Gurus,


Hello,


I am new to perl and need some help to learn regex in perl. From the
below line i need to extract following:

Part I
1) $ENVFROM = dapi...@testhost.com
2) $ENVTO1 = te...@etc.com
3) $ENVTO2 = te...@etc.com
4) $ENVTO3 = samt...@abc.com


line=EnvFrom: dapi...@testhost.com, HdrTo: ,
EnvTo: avis@test.com, te...@etc.com, samt...@abc.com, Subject:
TEST for perl


$ perl -le'
my $x = q[line=EnvFrom: dapi...@testhost.com, HdrTo: 
, EnvTo: avis@test.com, te...@etc.com, 
samt...@abc.com, Subject: TEST for perl];


my @fields = split /(\w+):\s+/, $x;

my %data;
for ( my $index = 0; $index <= $#fields; ++$index ) {
if ( $fields[ $index ] =~ /\Aenv(?:from|to)\z/i ) {
push @{ $data{ uc $fields[ $index ] } }, split /\s*,\s*/, 
$fields[ ++$index ];

}
}

use Data::Dumper;
print Dumper \%data;
'
$VAR1 = {
  'ENVFROM' => [
 'dapi...@testhost.com'
   ],
  'ENVTO' => [
   'avis@test.com',
   'te...@etc.com',
   'samt...@abc.com'
 ]
};



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: data and script in same location, how to write it down

2008-12-14 Thread John W. Krahn

Chas. Owens wrote:

On Sun, Dec 14, 2008 at 11:41, Mr. Shawn H. Corey  wrote:
snip

open my $fh, '<', $file or die "cannot open $file: $!\n";

snip

Completely off topic, but I dislike the error messages that say
"cannot".  "Cannot" implies that the problem exists currently and can
cause confusion when someone goes to check the error condition (which
sometimes no longer exists).  I prefer "Could not" which implies the
problem existed at the time the program ran, but might not exist now.
Or maybe I have worked with too many English majors.


You are talking about a microsecond (nanosecond?) interval between the 
error being detected and the message being displayed, of course the 
error condition no longer exists, the program just died!  Try working 
with us English corporals, those officers are too condescending.  :-)



John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Web scraping...move on if it fails

2008-12-14 Thread Chas. Owens
On Sat, Dec 13, 2008 at 13:26, hotkitty  wrote:
> HI,
>
> I have a bunch of news websites that are stored in my mysql db and
> each morning I have a script to go to each site and download the top
> stories (this is for personal use, not commercial). My problem is that
> sometimes www::mechanize will fail to get the website because the
> server is busy, or for whatever reason. HOw do I get it so that
> instead of dying right then and there it will just ignore it and move
> onto the next page?
snip

You can use a block eval* to catch the die the module is throwing.
This is similar to some languages try/catch syntax:


while ( $ref = $st->fetchrow_arrayref() ) {
print "Retrieving news stories from $page\n";
eval {
my $mech=WWW::Mechanize->new(autocheck => 1);
#this line dies if it cannot get to the web server
$mech->get($page);
#this line dies if it gets to the web server, but it
#doesn't get a good http status code back (e.g. a 404 or 403)
die $mech->status, " ", $mech->content unless $mech->status == 200;
print $mech->content, "\n\n\n";
1;
} or {
print "Could not get page: $@";
}
}

* http://perldoc.perl.org/functions/eval.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: data and script in same location, how to write it down

2008-12-14 Thread Chas. Owens
On Sun, Dec 14, 2008 at 11:41, Mr. Shawn H. Corey  wrote:
snip
> open my $fh, '<', $file or die "cannot open $file: $!\n";
snip

Completely off topic, but I dislike the error messages that say
"cannot".  "Cannot" implies that the problem exists currently and can
cause confusion when someone goes to check the error condition (which
sometimes no longer exists).  I prefer "Could not" which implies the
problem existed at the time the program ran, but might not exist now.
Or maybe I have worked with too many English majors.


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: data and script in same location, how to write it down

2008-12-14 Thread Chas. Owens
On Sun, Dec 14, 2008 at 11:36, Anirban Adhikary
 wrote:
> Just write the fiilename as us script and file to open are in same location.
> my $filename="datafile.txt ";
> open my $FH,'<', $filename || die "no such files $!\n";
snip

That needs to be

open my $FH, '<', $filename or die "could not open $filename: $!\n";

|| has to high an order of precedence.  Your code actually does this:

open(my $FH,'<', ($filename || die "no such files $!\n"));

Also, it opens the file in the current working directory, not in the
directory where the script is located.  See the other responses for
the right answer.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Regex pattern match in perl

2008-12-14 Thread Chas. Owens
On Sat, Dec 13, 2008 at 19:35, explor  wrote:
> Hi Gurus,
> I am new to perl and need some help to learn regex in perl. From the
> below line i need to extract following:
>
> Part I
> 1) $ENVFROM = dapi...@testhost.com
> 2) $ENVTO1 = te...@etc.com
> 3) $ENVTO2 = te...@etc.com
> 4) $ENVTO3 = samt...@abc.com
>
>
> line=EnvFrom: dapi...@testhost.com, HdrTo: ,
> EnvTo: avis@test.com, te...@etc.com, samt...@abc.com, Subject:
> TEST for perl
snip


If you can guarantee that EnvFrom:, EnvTo:, and Subject: will always
exist and that they will exist in the same order you can say
#!/usr/bin/perl

use warnings;
use strict;

my $line = 'line=EnvFrom: dapi...@testhost.com, HdrTo:
, EnvTo: avis@test.com, te...@etc.com,
samt...@abc.com, Subject: TEST for perl';

#assign captures from regex to $from and $to
my ($from, $to) = $line =~ m{
EnvFrom: #match starting at EnvFrom:
\s+  #follwed by one or more whitespace characters
(#begin first capture
[^\s,]+  #match one or more of any chacter but whitespace or ,
)#end first capture
.*   #match the largest string up to next anchor
EnvTo:   #which is the string EnvTo:
\s+  #follwed by one or more whitespace characters
(#begin second capture
.*?  #match the shortest string up to the next anchor
)#begin second capture
Subject: #which is the string Subject:
}xms;

#all of the addresses are still in one string for EnvTo:, so we
#split the on a comma followed by zero or more whitespace characters
#and store the resulting list into an array for easy access
my @to = split /,\s*/, $to;

print "from: $from\nto: ", join(", ", @to), "\n";

>
> Part II
> We have a huge log files with thousands of entries like above so might
> need to be done in either while  /for loop in the final phase. Also,
> the EnvTo: can be from one email address to multiple. In the above
> example there are 3 EnvTo but it can be more than 3 or can be one only
> but the regex should be able to determine and put each in a scalar
> variable.
>
> I do need the regex specific to Part I (This will help me learn the
> regex better)  and also separate regex for Part II if possible.

If each record is on a line by itself, there is no need for a separate regex:

#!/usr/bin/perl

use warnings;
use strict;

#read lines from either STDIN or a set of files passed in on the commandline
while (my $line = <>) {
#move to the next line if the regex doesn't match
next unless my ($from, $to) = $line =~ m{
EnvFrom: \s+ ([^\s,]+)
.*
EnvTo:   \s+ (.*?)
Subject:
}xms;

my @to = split /,\s*/, $to;

print "for record $. I found\n\tfrom: $from\n\tto: ",
join(", ", @to), "\n";
}

If each record is split over many lines you will need a different
solution depending on how the data is split.  If you post what your
data looks like we may be able to help.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: data and script in same location, how to write it down

2008-12-14 Thread Mr. Shawn H. Corey
On Sun, 2008-12-14 at 22:21 +0800, itshardtogetone wrote:
> Hi,
> I wish to open a file datafile.txt which is stored in the same location as 
> the perl script, so how do I write it down?
> 
> open (FILE,'<',"  ") || die "no such 
> files $!\n"; 
> 
> 

use File::Basename;
my $Script_dir = dirname( $0 );
my $file = "$Script_dir/datafile.txt";
open my $fh, '<', $file or die "cannot open $file: $!\n";



-- 
Just my 0.0002 million dollars worth,
  Shawn

The key to success is being too stupid to realize you can fail.


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: data and script in same location, how to write it down

2008-12-14 Thread Anirban Adhikary
Just write the fiilename as us script and file to open are in same location.
my $filename="datafile.txt ";
open my $FH,'<', $filename || die "no such files $!\n";

Regards
Anirban Adhikary.

On Sun, Dec 14, 2008 at 7:51 PM, itshardtogetone <
itshardtoget...@hotmail.com> wrote:

> Hi,
> I wish to open a file datafile.txt which is stored in the same location as
> the perl script, so how do I write it down?
>
> open (FILE,'<',"  ") || die "no
> such files $!\n";
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Special characters and variables in search and replace expressions of regexps

2008-12-14 Thread John W. Krahn

Alexei A. Frounze wrote:

Here's a sample program to show the problems I'm having with regexps:
my $s = "a\tb";


$s contains a three character string, the 'a' character, the TAB 
character and the 'b' character.



my $m1 = "\t";


$m1 contains a single TAB character.


my $m2 = "\\t"; # both "\t" and "\\t" are treated specially


$m2 contains two characters, the '\' character and the 't' character.


my $r1 = "\t";


Same as $m1.


my $r2 = "\\t";


Same as $m2.


my $s1 = $s;
my $s2 = $s;
$s1 =~ s/$m1/$r1/;


The same as: $s1 =~ s/\t/\t/; Replace a TAB character with a TAB character.


$s2 =~ s/$m1/$r2/;


The same as: $s1 =~ s/\t/\\t/; Replace a TAB character with the two 
character string '\t'.



print "\"$s\" =~ s/$m1/$r1/ -> \"$s1\"\n";
print "\"$s\" =~ s/$m1/$r2/ -> \"$s2\"\n";

my $s1 = $s;
my $s2 = $s;
$s1 =~ s/$m2/$r1/;


The same as: $s1 =~ s/\\t/\t/; Replace the two character string '\t' 
with a TAB character.



$s2 =~ s/$m2/$r2/;


The same as: $s1 =~ s/\\t/\\t/; Replace the two character string '\t' 
with the two character string '\t'.



print "\"$s\" =~ s/$m2/$r1/ -> \"$s1\"\n";
print "\"$s\" =~ s/$m2/$r2/ -> \"$s2\"\n";

$s = "aabcc";
$s1 = $s;
$s2 = $s;
my $m = "a(b)c"; # "(" is is treated specially, but "\\(" isn't
my $r = "$1";


perldoc -q quoting

Because capturing parentheses have not been used yet $1 will contain 
nothing so that is the same as:


my $r = "";

If you had warnings enabled then you would have been warned "Use of 
uninitialized value in string" at this point.


You probably want either:

my $r = "\$1";

Or:

my $r = '$1';

to prevent interpolation.

If you had used a regular expression with capturing parentheses before 
this point then $r would contain whatever was captured by them.



$s1 =~ s/$m/$r/; # $r (= "$1") is substituted with an empty string


The FAQ:

perldoc -q "How can I expand variables in text strings"

describes what you are trying to do here.


$s2 =~ s/$m/$1/; # $1 is substituted with "b"
print "\"$s\" =~ s/$m/\$r/ -> \"$s1\", where \$r=\"\$1\"\n";
print "\"$s\" =~ s/$m/\$1/ -> \"$s2\"\n";

It prints:
"a  b" =~ s//   / -> "a b"
"a  b" =~ s//\t/ -> "a\tb"
"a  b" =~ s/\t/ / -> "a b"
"a  b" =~ s/\t/\t/ -> "a\tb"
"aabcc" =~ s/a(b)c/$r/ -> "ac", where $r="$1"
"aabcc" =~ s/a(b)c/$1/ -> "abc"

The first problem (as you've probably already realized from the first
4 lines of the output) is that in the search expression both the tab
character "\t" and the backslash+t sequence "\\t" are treated the
same, as the tab character. Why is that?

The second problem is that in the replace expression the same backslash
+t sequence isn't treated "\\t" the same way as in the search
expression. Why is this inconsistency? Can I somehow force s/// to
treat "\\t" in replace as "\t"?

The third problem should be apparent from the last 2 lines of the
output. I want the $1 match (and possibly a few more) to come from
elsewhere (string data) and not be predetermined by the code. But it
doesn't seem to work this way. Is it possible to fix this?


You may want to read the sections "Quote and Quote-like Operators", 
"Regexp Quote-Like Operators" and "Gory details of parsing quoted 
constructs" in the perlop document:


perldoc perlop


John
--
Those people who think they know everything are a great
annoyance to those of us who do.-- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Web scraping...move on if it fails

2008-12-14 Thread hotkitty
HI,

I have a bunch of news websites that are stored in my mysql db and
each morning I have a script to go to each site and download the top
stories (this is for personal use, not commercial). My problem is that
sometimes www::mechanize will fail to get the website because the
server is busy, or for whatever reason. HOw do I get it so that
instead of dying right then and there it will just ignore it and move
onto the next page?

partial code below:


while ( $ref = $st->fetchrow_arrayref() )
{
print "Retrieving news stories from $page\n";
my $mech=WWW::Mechanize->new(autocheck => 1);
$mech->get($page) or warn "Cannot get page" and next;  this should
just throw a warning and then continue, but instead dies here#
my $content= $mech->content();
print "content\n\n\n";
}

THanks


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Regex pattern match in perl

2008-12-14 Thread explor
Hi Gurus,
I am new to perl and need some help to learn regex in perl. From the
below line i need to extract following:

Part I
1) $ENVFROM = dapi...@testhost.com
2) $ENVTO1 = te...@etc.com
3) $ENVTO2 = te...@etc.com
4) $ENVTO3 = samt...@abc.com


line=EnvFrom: dapi...@testhost.com, HdrTo: ,
EnvTo: avis@test.com, te...@etc.com, samt...@abc.com, Subject:
TEST for perl

Part II
We have a huge log files with thousands of entries like above so might
need to be done in either while  /for loop in the final phase. Also,
the EnvTo: can be from one email address to multiple. In the above
example there are 3 EnvTo but it can be more than 3 or can be one only
but the regex should be able to determine and put each in a scalar
variable.

I do need the regex specific to Part I (This will help me learn the
regex better)  and also separate regex for Part II if possible.

I am newbie to Perl and in the learning phase so please be descriptive
if possible in the answer. I greatly appreciate your help.


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




data and script in same location, how to write it down

2008-12-14 Thread itshardtogetone

Hi,
I wish to open a file datafile.txt which is stored in the same location as 
the perl script, so how do I write it down?


open (FILE,'<',"  ") || die "no such 
files $!\n"; 



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Special characters and variables in search and replace expressions of regexps

2008-12-14 Thread Alexei A. Frounze
Here's a sample program to show the problems I'm having with regexps:
my $s = "a\tb";

my $m1 = "\t";
my $m2 = "\\t"; # both "\t" and "\\t" are treated specially
my $r1 = "\t";
my $r2 = "\\t";

my $s1 = $s;
my $s2 = $s;
$s1 =~ s/$m1/$r1/;
$s2 =~ s/$m1/$r2/;
print "\"$s\" =~ s/$m1/$r1/ -> \"$s1\"\n";
print "\"$s\" =~ s/$m1/$r2/ -> \"$s2\"\n";

my $s1 = $s;
my $s2 = $s;
$s1 =~ s/$m2/$r1/;
$s2 =~ s/$m2/$r2/;
print "\"$s\" =~ s/$m2/$r1/ -> \"$s1\"\n";
print "\"$s\" =~ s/$m2/$r2/ -> \"$s2\"\n";

$s = "aabcc";
$s1 = $s;
$s2 = $s;
my $m = "a(b)c"; # "(" is is treated specially, but "\\(" isn't
my $r = "$1";
$s1 =~ s/$m/$r/; # $r (= "$1") is substituted with an empty string
$s2 =~ s/$m/$1/; # $1 is substituted with "b"
print "\"$s\" =~ s/$m/\$r/ -> \"$s1\", where \$r=\"\$1\"\n";
print "\"$s\" =~ s/$m/\$1/ -> \"$s2\"\n";

It prints:
"a  b" =~ s//   / -> "a b"
"a  b" =~ s//\t/ -> "a\tb"
"a  b" =~ s/\t/ / -> "a b"
"a  b" =~ s/\t/\t/ -> "a\tb"
"aabcc" =~ s/a(b)c/$r/ -> "ac", where $r="$1"
"aabcc" =~ s/a(b)c/$1/ -> "abc"

The first problem (as you've probably already realized from the first
4 lines of the output) is that in the search expression both the tab
character "\t" and the backslash+t sequence "\\t" are treated the
same, as the tab character. Why is that?

The second problem is that in the replace expression the same backslash
+t sequence isn't treated "\\t" the same way as in the search
expression. Why is this inconsistency? Can I somehow force s/// to
treat "\\t" in replace as "\t"?

The third problem should be apparent from the last 2 lines of the
output. I want the $1 match (and possibly a few more) to come from
elsewhere (string data) and not be predetermined by the code. But it
doesn't seem to work this way. Is it possible to fix this?

Thanks,
Alex


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Web scraping...move on if it fails

2008-12-14 Thread Filip van der Meeren
I am not familiar with the package, but personally I would place it in  
a eval block.


Filip van der Meeren
fi...@code2develop.com
http://www.sourceforge.net/projects/perlmanager
http://www.sourceforge.net/projects/xlinterpreter

On 13 Dec 2008, at 19:26, hotkitty wrote:


HI,

I have a bunch of news websites that are stored in my mysql db and
each morning I have a script to go to each site and download the top
stories (this is for personal use, not commercial). My problem is that
sometimes www::mechanize will fail to get the website because the
server is busy, or for whatever reason. HOw do I get it so that
instead of dying right then and there it will just ignore it and move
onto the next page?

partial code below:


while ( $ref = $st->fetchrow_arrayref() ) { print "Retrieving news
stories from $page\n"; my $mech=WWW::Mechanize->new(autocheck => 1);
$mech->get($page) or warn "Cannot get page" and next;  this should
just throw a warning and then continue, but instead dies here# my
$content= $mech->content(); print "content\n\n\n"; }

THanks


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/





--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Web scraping...move on if it fails

2008-12-14 Thread hotkitty
HI,

I have a bunch of news websites that are stored in my mysql db and
each morning I have a script to go to each site and download the top
stories (this is for personal use, not commercial). My problem is that
sometimes www::mechanize will fail to get the website because the
server is busy, or for whatever reason. HOw do I get it so that
instead of dying right then and there it will just ignore it and move
onto the next page?

partial code below:


while ( $ref = $st->fetchrow_arrayref() ) { print "Retrieving news
stories from $page\n"; my $mech=WWW::Mechanize->new(autocheck => 1);
$mech->get($page) or warn "Cannot get page" and next;  this should
just throw a warning and then continue, but instead dies here# my
$content= $mech->content(); print "content\n\n\n"; }

THanks


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




using Curl in a package

2008-12-14 Thread Bucker
Hi Folks

if i take the code outside a package it works perfectly.
Once put into a package i do the following which works great

my $get_account = accounts->new($ss_username,$ss_password);

the call works fine and getAccountInfo gets called.


The only thing that isn't working is the call to ret_data2 once curl
is complete.
The code works outside a package..just not within a package...the
ret_data2
is never called even though $curl->errbuff is 0.

Your thoughts?


use WWW::Curl::Easy;
use Data::Dumper;
use XML::DOM;
{ package accounts;
  my $ss_username;
  my $ss_account_id;
  my $ss_email_address;
  my $ss_password;

  my $self = { ss_username => $ss_username, ss_password =>
$ss_password, ss_account_id => $ss_account_id, ss_email_address =>
$ss_email_address };  # Anonymous hash reference holds instance
attributes

  sub new {
  my($class, $ss_username,$ss_password) = @_;# Class name is
in the first parameter


  bless($self, $class);  # Say: $self is a $class


  my $ret_list = getAccountInfo();
  print $ret_list."\n";
 return $self;

  }

  sub getAccountInfo {
 my @authHeader = ('Accept: application/xml', 'Content-Type:
application/xml');
# Setting the options
my $curl = new WWW::Curl::Easy;
 $curl->setopt(CURLOPT_HEADER,0);
 $curl->setopt(CURLOPT_HTTPHEADER, \...@authheader);
 $curl->setopt(CURLOPT_SSL_VERIFYPEER, 0);
 $curl->setopt(CURLOPT_USERPWD, 'myusername:mypassword');
 $curl->setopt(CURLOPT_URL, "https://app.streamsend.com/
audiences");
 $curl->setopt(CURLOPT_RETURNTRANSFER,1);

 $curl->setopt(CURLOPT_WRITEFUNCTION,\&ret_data2);
 my $result =  $curl->perform();
 my $err = $curl->errbuff;
 print "err = ".$err."\n";
 $curl->dispose();


  }
 

 #  ret_data2
 #  is called once curl is completed
 #  the xml statement above is processed
 #  we only need the id, and email address
 

sub ret_data2 {
 print "ret_data\n";
 my $chunk = shift;  #this will get the xml
 print $chunk;
 $xp = new XML::DOM::Parser();
 $doc = $xp->parse($chunk);
 $self->{ss_account_id} =  $doc->getElementsByTagName("id")->item(0)-
>getFirstChild->getNodeValue;
 $self->{ss_email_address} =  $doc->getElementsByTagName("from-email-
address")->item(0)->getFirstChild->getNodeValue;
 return 0;

   }
}
1;


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: PDF::API2

2008-12-14 Thread Dermot
2008/12/12 ChrisC :
> Hi!

Hi,

> How to underline text using PDF::API2?
>
> Currently using following to print text:
>
>   sub print_pdf_text {
>my $xp   = shift;
>my $yp   = shift;
>my $fnt  = shift;
>my $Mfnt = shift;
>my $text = shift;
>
>$txt->font( $fnt, $Mfnt);
>$txt->translate( $xp, $yp -= 12 );
>$txt->text( $text );
>  }
>

Have a look at

http://search.cpan.org/~areibens/PDF-API2-0.72.003/lib/PDF/API2/Content.pm

$width = $txt->text $text, %options
Applys text to the content and optionally returns the width of the
given text.

Options

-indent
Indent the text by the number of points.

-underline
If this is a scalar, it is the distance, in points, below the
baseline where the line is drawn. The line thickness is one point. If
it is a reference to an array, each pair is the distance below the
baseline and the thickness of the line (ie., -underline=>[2,1,4,2]
will draw a double underline with the lower twice as thick as the
upper).

If thickness is a reference to an array, the first value is the
thickness and the second value is the color of the line (ie.,
-underline=>[2,[1,'red'],4,[2,'#ff']] will draw a ``red'' and a
``blue'' line).

You can also use the string 'auto' for either or both distance and
thickness values to auto-magically calculate best values from the
font-definition.


Good Luck,
Dp.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/