Query Online File Size

2009-11-26 Thread raphael()
Hi,

I am writing a small script to download files of the web.
How can I get the file size without downloading the file?

use LWP::Simple;
my $file = http://www.abc.com/file.mp3;
my @array = head($file);
print $array[1]\n;

head() doesn't always returns all values?  why??
Sometime there are all values some time @array is empty!
Should I try LWP::UserAgent or is there any other way?


Re: Query Online File Size

2009-11-26 Thread Giany
use LWP::UserAgent;

sub GetFileSize{
my $url=shift;
$ua = new LWP::UserAgent;
$ua-agent(Mozilla/5.0);
my $req = new HTTP::Request 'HEAD' = $url;
$req-header('Accept' = 'text/html');
$res = $ua-request($req);
if ($res-is_success) {
 my $headers = $res-headers;
 return $headers;
}
return 0;
}


$link='http://www.abc.com/file.mp3';
$header = GetFileSize($link);

print File size: .$header-content_length. bytes\n;
exit;




On Thu, Nov 26, 2009 at 12:28 PM, raphael() raphael.j...@gmail.com wrote:

 Hi,

 I am writing a small script to download files of the web.
 How can I get the file size without downloading the file?

 use LWP::Simple;
 my $file = http://www.abc.com/file.mp3;
 my @array = head($file);
 print $array[1]\n;

 head() doesn't always returns all values?  why??
 Sometime there are all values some time @array is empty!
 Should I try LWP::UserAgent or is there any other way?



Assignment Operator

2009-11-26 Thread Marco Pacini
Hi All,

I'm studying Perl since one week on Learning Perl written by L. Wall and in 
the paragraph Assignment Operators i don't understand why this:

($temp = $global) += $constant;

is equivalent of:

$tmp = $global + $constant;

Instead,  before i read it, i thought it was equivalent of:

$temp = $global;
$temp = $temp + $constant;

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





Re: Assignment Operator

2009-11-26 Thread Philip Potter
2009/11/26 Marco Pacini i...@marcopacini.org:
 Hi All,

 I'm studying Perl since one week on Learning Perl written by L. Wall and in 
 the paragraph Assignment Operators i don't understand why this:

        ($temp = $global) += $constant;

 is equivalent of:

        $tmp = $global + $constant;

 Instead,  before i read it, i thought it was equivalent of:

        $temp = $global;
        $temp = $temp + $constant;

Let's relabel the first value of $temp as a new variable, $temp2:

my $temp2 = $global;
$temp = $temp2 + $constant;

Notice that $temp2 has just been given the value of $global, so this
second statement is the same as:

$temp = $global + $constant;

In other words, you're both right, because your version is equivalent
to $temp = $global + $constant.

Philip

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




udp server

2009-11-26 Thread cerr
Hi There,

I need a server that's listening on UDP port 5000.
I have successfully gotten a TCP client to listen on port 5000:
[perl]
#!/usr/bin/perl -w
# serverfork.pl - a server that forks a child
# process to handle client connections
use strict;
use IO::Socket;
use Sys::Hostname;
use POSIX qw(:sys_wait_h);
sub REAP {
1 until (-1 == waitpid(-1, WNOHANG));
$SIG{CHLD} = \REAP;
}
$SIG{CHLD} = \REAP;
my $sock = new IO::Socket::INET(
   LocalHost = 'localhost',
   LocalPort = 5000,
   Proto = 'tcp',
   Listen= SOMAXCONN,
   Reuse = 1);
$sock or die no socket :$!;
STDOUT-autoflush(1);
my($new_sock, $buf, $kid);
print listening on port 5000: $sock\n;
while ($new_sock = $sock-accept()) {
  print executing fork\n;
# execute a fork, if this is
# the parent, its work is done,
# go straight to continue
next if $kid = fork;
die fork: $! unless defined $kid;
# child now...
# close the server - not needed
close $sock;
while (defined($buf = $new_sock)) {
   chop $buf;
   print buf: $buf\n;
}
exit;
} continue {
print will close socket: $new_sock\n;
# parent closes the client since
# it is not needed
close $new_sock;
}

[/perl]
Now how do I modify this same code to listen for udp packets instead
of tcp ones?
Thank you,
Ron


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




Perl - Read and Write Multi-Language text (Scandanavian,German)

2009-11-26 Thread CM
Deal all,

INFO uname -a = Linux perlsrv 2.4.27 #1 Tue Sep 23 15:34:56 CEST
2008 i686 GNU/Linux
INFO perl -v = This is perl, v5.8.4 built for i386-linux-thread-
multi

I  am seeking advice on  how to  generate multi-language output in
Perl.

The task here is to generate a single page of HTML, PDF and  plain
text files with each run of the Perl script.
Each of the above has to be in the language  chosen by the user in a
web-interface.
The possible choices are : Danish, Finnish , Swedish
Based on the language, I would like to read  text from a file
containing language specific characters (like AE and umlaut ) and
generate PDF,HTML and plain text output.



My questions are:
- How do I know if my OS supports printing of these languages
- How I can type in text in a editor like  vi with Finnish/Danish/Swed
characters
- How do I enable Perl to understand that a file contains language
specific characters.
- And finally what should I do to ensure that the output produced by
Perl : PDF, HTML  and plain text files dont mess up the  special
characters


Any advice on this is much appreciated. I know I can do  the file read/
write operations and output generation parts with Perl with some
effort but the language part seems quite  a challenge.

I am in a situation where I do not know where to  start.










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




Re: Perl - Read and Write Multi-Language text (Scandanavian,German)

2009-11-26 Thread Shlomi Fish
On Wednesday 25 Nov 2009 15:15:19 CM wrote:
 Deal all,
 
 INFO uname -a = Linux perlsrv 2.4.27 #1 Tue Sep 23 15:34:56 CEST
 2008 i686 GNU/Linux
 INFO perl -v = This is perl, v5.8.4 built for i386-linux-thread-
 multi

1. Linux Kernel 2.4.27 is incredibly old:

http://en.wikipedia.org/wiki/Linux_kernel#Timeline

There's already kernel 2.4.37 and kernel 2.6.31.6. Please upgrade, for your 
own good.

2. perl-5.8.4 is very old as well. There's already 5.8.9 and 5.10.1.

---

Which system is this? Is it RHEL / CentOS ?

I think both of these should not pose a problem in what I'm saying.

 
 I  am seeking advice on  how to  generate multi-language output in
 Perl.
 
 The task here is to generate a single page of HTML, PDF and  plain
 text files with each run of the Perl script.
 Each of the above has to be in the language  chosen by the user in a
 web-interface.
 The possible choices are : Danish, Finnish , Swedish
 Based on the language, I would like to read  text from a file
 containing language specific characters (like AE and umlaut ) and
 generate PDF,HTML and plain text output.
 
 
 
 My questions are:
 - How do I know if my OS supports printing of these languages

Look at your distribution's documentation. It should make use of CUPS ( 
http://www.cups.org/ , 
http://en.wikipedia.org/wiki/Common_Unix_Printing_System ) or something.

 - How I can type in text in a editor like  vi with Finnish/Danish/Swed
 characters

You can either use the compose key or use vim's (not vi's) Ctrl+K feature:

http://vimdoc.sourceforge.net/search.php?search=digraphsdocs=help

I have the following mapping for the compose key:


setxkbmap \
-option compose:ralt,grp:switch,grp:alt_shift_toggle,grp_led:scroll  \
-variant ,lyx \
'us,il'


Note that it also defines a hebrew keyboard which is less useful for you. You 
probably want only (untested):


setxkbmap -option compose 'us'


 - How do I enable Perl to understand that a file contains language
 specific characters.

See:

* http://perldoc.perl.org/perlunitut.html

 - And finally what should I do to ensure that the output produced by
 Perl : PDF, HTML  and plain text files dont mess up the  special
 characters
 

See above.

 
 Any advice on this is much appreciated. I know I can do  the file read/
 write operations and output generation parts with Perl with some
 effort but the language part seems quite  a challenge.
 
 I am in a situation where I do not know where to  start.
 

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Parody on The Fountainhead - http://shlom.in/towtf

Chuck Norris read the entire English Wikipedia in 24 hours. Twice.

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




Re: loading GD module on Solaris

2009-11-26 Thread Paul Johnson
On Thu, Nov 26, 2009 at 12:15:06AM +0100, Paul Johnson wrote:
 On Wed, Nov 25, 2009 at 03:41:27PM -0600, Aimee Cardenas wrote:
 
  Hi, All,
 
  I keep running into problems trying to load the GD perl module 
  (http://search.cpan.org/~lds/GD-2.44/) onto a Solaris system (spac Sun 
  Blade 2500).  Does anyone know if it's just not feasible?  The GD README 
  mentions Mac OS X and Linux and even Windows but not Solaris.  The 
  problems are with cc.  Since Solaris has it's own version C compiler, I'm 
  sure this is the problem but does anyone know if there's a page somewhere 
  discussing how someone got the GD module to work on Solaris?  I really 
  appreciate your help on this.
 
  btw, my present error is:
 
  cc: acomp failed for GD.c
 
 That was just the last part though, I presume there was a useful error
 message earlier.  In any case, this is possible, I did it just last
 week.  However, it doesn't work out of the box.  I don't remember the
 exact problem and fix offhand (I had to fix up a few modules to get them
 to build on solaris), but I *think* there are some gcc specific flags in
 the Makefile that you'll need to deal with.  I presume you have the GD
 library installed properly.
 
 If you need a bit more I can get you the exact fix tomorrow.

Just for completeness, my minimal solution is to run:

  make CCFLAGS=

And yes, this really should be propagated upstream.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

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




Re: Perl - Read and Write Multi-Language text (Scandanavian,German)

2009-11-26 Thread Shlomi Fish
Hi Chandramohan!

I'm CCing this list on this private reply. Next time please hit reply all or 
make it the default using the Google Labs interface.

On Thursday 26 Nov 2009 16:29:36 Chandramohan Neelakantan wrote:
 Hi Schlomi,
 

It's Shlomi (English spelling) - not Schlomi (German Spelling). Many 
people make this mistake.

 Many thanks for your reply.

You're welcome.

 
  1. Linux Kernel 2.4.27 is incredibly old:
  http://en.wikipedia.org/wiki/Linux_kernel#Timeline
  There's already kernel 2.4.37 and kernel 2.6.31.6. Please upgrade, for
  your own good. 2. perl-5.8.4 is very old as well. There's already 5.8.9
  and 5.10.1. ---
 
 I  do not have an option here to change/upgrade the OS.
 

I see. We may not be able to help you with problems you encounter.

  Which system is this? Is it RHEL / CentOS ?
  I think both of these should not pose a problem in what I'm saying.
 
 Debian.
 

Really? What was the last Debian that shipped with these versions of Perl and 
the Linux kernel?

What does cat /etc/debian_version say?

This seems likely that it would be an old and un-maintained version. See:

http://community.livejournal.com/shlomif_tech/36125.html

  See:
  * http://perldoc.perl.org/perlunitut.html
 
 I  have been  going through the documentation for the last few days now.
 Unfortunately I am in a unique position.
 The  text for me comes various sources : which means  that
 standardization of everything is also not an option as there are many
 many parties here.
 (For example, I would have a non-Danish speaker to write Danish  text
 and  send it to me).
 
 I hit on an idea that each text file coming from different sources will
 have the Unicode UTF8 hex string of all the special characters.
 See here. http://lwp.interglacial.com/appf_01.htm
 
 For example the Danish A-ring ( A with a ring on top ) in the text file
 will be written as : 0xc30x85. This was found to be acceptable by all
 parties.
 
 Now the question is,  I will have a text file with lots of English
 langauge alphabets along with the special characters written as above.
 Using File i/o, I will have to  dynamically identify the special
 characters and print the equiavalent characters in PDF,HTML files.
 
 Any ideas?
 

You can use regexes to match specific characters. But generally your converter 
(e.g: of a http://en.wikipedia.org/wiki/Lightweight_markup_language ) wil ldo 
that for you. Do you need to guess the language of the document? I still don't 
understand exactly what you want to do.

Regards,

Shlomi Fish

 Your help much appreciated.
 
 Thanks  regards
 CM
 

-- 
-
Shlomi Fish   http://www.shlomifish.org/
The Human Hacking Field Guide - http://shlom.in/hhfg

Chuck Norris read the entire English Wikipedia in 24 hours. Twice.

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




Re: Assignment Operator

2009-11-26 Thread Uri Guttman
 MP == Marco Pacini i...@marcopacini.org writes:

  MP I'm studying Perl since one week on Learning Perl written by
  MP L. Wall and in the paragraph Assignment Operators i don't
  MP understand why this:

it was written by randal schwartz. larry wall co-wrote programming perl.

  MP   ($temp = $global) += $constant;

  MP is equivalent of:

  MP   $tmp = $global + $constant;

  MP Instead,  before i read it, i thought it was equivalent of:

  MP   $temp = $global;
  MP   $temp = $temp + $constant;

they do the same thing so why do you care?

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

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




Separating DB operations out of program code

2009-11-26 Thread Steve Bertrand
Hi everyone,

I'm looking to separate all of my MySQL logic (queries etc) out of my
projects methods and into it's own class.

Primarily, this is to provide me with the ability to change how the
back-end is interacted with, without having to go through each module
and make the changes there. For instance, I am about to add code so that
 the project can use multiple SQL servers if a config variable is set.

The idea is that the project subs will pass along the SQL criteria to a
'wrapper' method in a dedicated class which does the actual DBI work.
After the separation is complete, it will be trivial to simply wrap the
wrapper in a foreach statement and iterate the transaction $num_servers
times.

Can anyone provide any experience/insights as to the best way to
accomplish this, or provide the name of some CPAN modules that I can
learn from (or use).

Steve

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




Re: Separating DB operations out of program code

2009-11-26 Thread Scott Pham

Have you looked at DBIx::Class?

Steve Bertrand wrote:

Hi everyone,

I'm looking to separate all of my MySQL logic (queries etc) out of my
projects methods and into it's own class.

Primarily, this is to provide me with the ability to change how the
back-end is interacted with, without having to go through each module
and make the changes there. For instance, I am about to add code so that
 the project can use multiple SQL servers if a config variable is set.

The idea is that the project subs will pass along the SQL criteria to a
'wrapper' method in a dedicated class which does the actual DBI work.
After the separation is complete, it will be trivial to simply wrap the
wrapper in a foreach statement and iterate the transaction $num_servers
times.

Can anyone provide any experience/insights as to the best way to
accomplish this, or provide the name of some CPAN modules that I can
learn from (or use).

Steve

  



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




Re: Separating DB operations out of program code

2009-11-26 Thread Dermot
2009/11/26 Scott Pham scott.p...@gmail.com:
 Have you looked at DBIx::Class?


I'd 2nd that. DBIx is the way forward. You should be looking to stop
writing SQL statements and moving towards ORM. Try the example at
http://search.cpan.org/~frew/DBIx-Class-0.08114/lib/DBIx/Class/Manual/Example.pod

Good luck,
Dp.

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




calc time elapsed in days

2009-11-26 Thread raphael()
Hi,

I have to code a script to calc time elapsed in days as to calc the rent to
be charged.
The person would write an object number and date in a text file like

db.txt
OBJECT_NUM, DATE_GIVEN

2525,25.11.2008
2526,01.01.2009
2527,26.11.2009

Now when he enter OBJECT_NUM I have to tell him the numbers of days
elapsed since DATE_GIVEN
and the rent he should charge which increases every 15 days like

first 15 days = 2
next 15 days = 3
next 15 days = 5
next 15 days = 8
next 15 days = 10
now its always 10

So if the OBJECT is returned in say 40 days the rent charged is 10 (2 + 3 +
5)

Being a beginner, I thought of hard coding the values in the code.

if ( $days_elapsed = 15 ) {
$total_cost = 2;
} elsif ( $days_elapsed = 30 ) {
$total_cost = 5;
} elsif ( $days_elapsed = 45 ) {
$total_cost = 10;
} elsif ( $days_elapsed = 60 ) {
$total_cost = 18;
} elsif ( $days_elapsed = 75 ) {
$total_cost = 28;
} elsif ( $days_elapsed = 90 ) {
$total_cost = 38;
} else {
print More than 90 days! SPECIAL RATE\n;
exit;
}


HOW CAN I COUNT ELAPSED DAYS ?

I tried in localtime() days value like

my @array_date = localtime();
my $current_dayofyear = @array_date[7];


But this would break on New year as $current_dayofyear would be reset.


SO HOW CAN I COUNT ELAPSED DAYS IF THE DATE FORMAT IS  01.01.2009?