perl, saving the command line option - perl

I have this perl script and it takes in arguments using the getoption package.
Is there an easy way to document what was the exact command the user used to execute?
I would like to document into a log file.
Gordon

Use $0 and #ARGV together:
my $full_command = join(' ', $0, #ARGV);

or you can simply
my $full;
BEGIN { $full = "$0 #ARGV" }
#
print "log: $full\n";
form the perlvar
$LIST_SEPARATOR
$"
When an array or an array slice is
interpolated into a double-quoted
string or a similar context such as
/.../ , its elements are separated by
this value. Default is a space. For
example, this:
print "The array is: #array\n";
is equivalent to this:
print "The array is: " . join($", #array) . "\n";

Related

accessing perl scalar variables in ()

$a=10
print "$a";
print "${a}";
print "$(a)";
print "$a"; and print "${a}"; work exactly same way, looks like both syntax are supported in perl to print scalar variables, but print "$(a)"; prints strange values , Want to undersand what happens when print "$(a)";
$( is a valid scalar in Perl. It contains the space-separated list of group ids that the current user belongs to.
So print "$(a)" is equivalent to print $( . "a)" and you can expect output like
perl -e 'print "$(a)"'
100 100 14677a)

print lines after finding a key word in perl

I have a variable $string and i want to print all the lines after I find a keyword in the line (including the line with keyword)
$string=~ /apple /;
I'm using this regexp to find the key word but I do not how to print lines after this keyword.
It's not really clear where your data is coming from. Let's assume it's a string containing newlines. Let's start by splitting it into an array.
my #string = split /\n/, $string;
We can then use the flip-flop operator to decide which lines to print. I'm using \0 as a regex that is very unlikely to match any string (so, effectively, it's always false).
for (#string) {
say if /apple / .. /\0/;
}
Just keep a flag variable, set it to true when you see the string, print if the flag is true.
perl -ne 'print if $seen ||= /apple/'
If your data in scalar variable we can use several methods
Recommended method
($matching) = $string=~ /([^\n]*apple.+)/s;
print "$matching\n";
And there is another way to do it
$string=~ /[^\n]*apple.+/s;
print $&; #it will print the data which is match.
If you reading the data from file, try the following
while (<$fh>)
{
if(/apple/)
{
print <$fh>;
}
}
Or else try the following one liner
perl -ne 'print <> and exit if(/apple/);' file.txt

perl printing variables

The common code is :
use strict;
use warnings;
my $f = $ARGV[0];
use Bio::EnsEMBL::Registry;
use Bio::EnsEMBL::ApiVersion;
my $registry = 'Bio::EnsEMBL::Registry';
$registry->load_registry_from_db(
-host => 'ensembldb.ensembl.org',
-user => 'anonymous',
-port => '5306'
);
my $adaptor = $registry->get_adaptor( 'Homo sapiens', 'core', 'transcript' );
my $transcript =
$adaptor->fetch_by_translation_stable_id($f);
LAST LINE
#
For the LAST LINE, I am having trouble printing out the two values as two columns in the same line :
Attempt 1 code: print $f . $transcript->display_id. "\n";
Result : api.test.pl ENSP00000418690
ENSP00000418690ENST00000488594
Attempt 2 code :print $f, $transcript->display_id. "\n";
Result : perl api.test.pl ENSP00000418690 :
ENSP00000418690ENST00000488594
Any other attempt messes with accessing the display_id. What I want the format to be is :
ENSP00000418690 ENST00000488594
If you want a space between the values, print a space between the values.
print $f, " ", $transcript->display_id, "\n";
The dot joins two strings into one string, which isn't necessary here, because print accepts a comma-separated list of values.
Just to mention another possibility, you can two built-in perl variables.
{
local $, = " "; # Output field separator
local $\ = "\n"; # Output record separator
print $f, $transcript->display_id;
}
The output field separator is automatically printed between each argument in the list passed to print. The output record separator is printed after the final argument to print. Both are undefined by default, and interpreted to mean "don't print anything".
I declared them as local to a block as the simplest way of restricting them to the single print statement.

Perl Array values show in different line without using loop

Could anyone tell me how could I print values of array in different rows without using loop?
#!/usr/bin/perl -w
my #a = ('Test1','Test2','Test3');
print "#a";# output like **Test1 Test2 Test3** but i want **Test2 in next line and Test3 next to next line**
Is it Possible?
You can just do:
print join("\n", #ar);
You can set $" variable
$" = "\n";
It's probably better to do
{
local $" = "\n";
print "#ar";
}
EDIT:
according to the camel book :
$" (or the alternative $LIST_SEPERATOR) specifies the string to put between individual elements when an array is interpolated into a double-quoted string, this for the case you want to say:
print "#ar";
$, (or the alternative $OUTPUT_FIELD_SEPERATOR) specifies the string to put between individual elements when you want to print a list. It's initially empty. You can set $, for the case you want to say:
print #ar;
You can set the $, special variable to be whatever you want to separate your list elements. This should do what you want:
$, = "\n";
my #a = ('Test1','Test2','Test3');
print #a;
use map function
print #array = map{"$_\n"} #a;

perl + #ARGV + print syntax

How to print the number of arguments from #ARGV
according to the following script why its important to print
like
print q{don't have parameters};
And not as
print "don't have parameters"; ??
lidia
#!/usr/bin/perl
if (#ARGV) {
print ......
} else {
print q{don't have parameters};
}
To print the number of elements in any array in perl:
print scalar(#ARGV);
Using q{} OR single quotes '' means that a string will get quoted but NOT interpolated, meaning any variables you have inside will not have their actual values. This is a faster way to create strings than with double quotes "" or qq{} which WILL interpolate variables in the string.
moreover, print q{} is a shorthand for :
print 'don\'t have parameters'
double quotes mean your string gets interpolated. ie : perl analysis it to retrieve
variable values.
simple quotes won't. No unrequired analysis -> faster, less memory/cpu/whatever usage