Strip Out The Last URL Name In String - perl

I need to strip out the last page name in a variable.
Here is my code so far but I loose the .jpg extension and I need to preserve it, Needed result: ImageName256.jpg
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
$Variable = "http://www.MyDomain.com/SomefolderPath/ImageName256.jpg";
($LastInUrl) = $Variable =~ m(.*/(\w+));
print $LastInUrl;

I usually avoid regexes for this sort of thing and handle it like this:
#!/usr/bin/perl
use strict;
use warnings;
use URI;
use URI::Escape qw( uri_unescape );
use File::Basename;
my $variable = "http://www.MyDomain.com/SomefolderPath/ImageName256.jpg";
my $last_in_url = uri_unescape( basename( URI->new( $variable )->path ) );
print $last_in_url;
Note: File::Basename's behaviour changes depending on which system it's used one. You'd have to use the following if you wanted portability:
my $fstype = fileparse_set_fstype('uri');
my $last = uri_unescape( basename( $uri->path ) );
fileparse_set_fstype($fstype);

use URI qw( );
my $uri = "http://www.MyDomain.com/SomefolderPath/ImageName256.jpg";
$uri = URI->new($uri);
my $basename = ( $uri->path_segments )[-1];
If you wanted to continue using a regex, you'd have to use the following:
use URI::Escape qw( uri_unescape );
my $uri = "http://www.MyDomain.com/SomefolderPath/ImageName256.jpg";
my $basename = $uri =~ m{/([^/#?]*)(?=[#?]|\z)} ? uri_unescape($1) : '';
Obviously, I strongly recommend the first solution over the second.

($LastInUrl) = (split qr{/}, $Variable)[-1];

Related

How to substitute " with " in perl

I want to substitute all " in the string $input with /" and I come up with the following code:
#!/usr/bin/perl -w
use CGI;
use CGI::Carp qw ( warningsToBrowser fatalsToBrowser );
my $q = CGI -> new;
print $q -> header();
$input = 'abc"abc';
(my $output = $input) =~ s/"/"/g;
print "input = $input\n";
print "output = $output\n";
However, the $output is not changed. How can I get this right?
You should use a module suitable for the purpose, such as HTML::Entities:
use strict;
use warnings;
use HTML::Entities;
my $input = 'abc"abc';
print encode_entities($input);
Output:
abc"abc
In your code, the variable $input is not changed because you are using parentheses to avoid it.
(my $output = $input) =~ s/"/"/g;
This will enforce the assignment to happen first, overruling precedence. Then the regex substitution is applied to $output. When I run your code, I get the expected output:
input = abc"abc
output = abc"abc
If you do not get this output, I expect the quotes in your string is something different than you think.

How to parse a key - value based dictionary using Perl

using Perl I get a key - value based dictionary from an API call.
use strict;
use warnings;
use LWP::Simple;
my $url = "http://example.com/?id=124341";
my $content = get($url);
$content =~ s/ /%20/g;
print $content;
{"id":"85710","name":"jack","friends":["james","sam","Michael","charlie"]}
how can I parse it to get the results as bellow?
name : jack
hist friend list :
james
sam
Michael
charlie
Thanks!
use strict;
use warnings;
use JSON 'decode_json';
my $content = '{"id":"85710","name":"jack","friends":["james","sam","Michael","charlie"]}';
my $person = decode_json($content);
print "name : $person->{'name'}\n";
print "his friend list :\n";
for my $friend ( #{ $person->{'friends'} } ) {
print "$friend\n";
}
use JSON; # imports encode_json, decode_json, to_json and from_json.
my $href = decode_json($content);
use Data::Dumper; print Dumper $href;
use JSON::Tiny 'j';
my $data = j $content;
printf <<TEMPLATE, $data->{name}, join( "\n", #{ $data->{friends} } );
name : %s
his friend list:
%s
TEMPLATE

Does somebody know a module, which has a function that returns a kb/s downloadspeed?

Does somebody know a module, which has a function that returns ( for example for eth0 ) the download speed?
Employ a monitoring program: atop, iftop, ntop, dstat, icinga, munin, knemo, ksysguardd
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes;
use LWP::Simple;
my $url = 'http://www.cnn.com/';
my $file = 'cnn.html';
my $start = [Time::HiRes::gettimeofday()];
getstore($url, $file);
my $time = Time::HiRes::tv_interval($start);
my $size = -s $file;
printf "Speed: %d kbps\n", $size/$time/1000;

perl hash declaration error

I am new to perl and can't seem to find why this snippet is giving me a 500 error.
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw( fatalsToBrowser );
my ($distance, $weight, $total_gas, $mph, $buffer, $pair, #pairs, $value, $form, $name);
our %FORM = ();
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#pairs = split(/&/, $buffer);
foreach $pair (#pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
Everything I tried on the %FORM = (); gives me variable declaration errors.
Are you certain that #pairs contains the values you expect (ie that they are name value pairs split with "="? More than likely $name isn't defined and you can't add an undefined key pair to a hash. Why are you using STDIN to read in values from the query string? Try:
my $q = CGI->new;
my #keys = $q->param;
my %FORM;
foreach my $name (#keys)
{
my $value = $q->param($name);
$FORM{$name} = $value;
}
or
my $q = CGI->new;
my %FORM = $q->Vars;
http://perldoc.perl.org/CGI.html
I think you're missing HTTP header. Try adding to put following line before any print:
print "Content-type: text/html\n\n";
Make sure you have enough permissions for script to run. It'll depend on OS you're using.
Also you'd consider using CGI module as mentioned in scrappedcola's answer. This code will work for both POST and GET:
use strict; use warnings;
use CGI;
my $form = CGI->Vars;
print "Content-type: text/html\n\n";
print "name=".$form->{name};

What is the right way to stop an infinite while-loop with a Term::Readline-readline?

What is the right way to stop an endless while-loop with a Term::Readline::readline?
This way I can not read in a single 0
#!/usr/bin/env perl
use warnings;
use strict;
use 5.010;
use Term::ReadLine;
my $term = Term::ReadLine->new( 'Text' );
my $content;
while ( 1 ) {
my $con = $term->readline( 'input: ' );
last if not $con;
$content .= "$con\n";
}
say $content;
and with
last if not defined $con;
the loop does never end.
You can do it the way it is shown in the documentation:
use strict; use warnings;
use Term::ReadLine;
my $term = Term::ReadLine->new('Text');
my $content = '';
while ( defined (my $con = $term->readline('input: ')) ) {
last unless length $con;
$content .= "$con\n";
}
print "You entered:\n$content\n";
Output:
C:\Temp> t
input: one
input: two
input:^D
You entered:
one
two