size" in perl? I would like to know how to print the value of a member sizefrom an object Fil...">

What is the correct way: print "File size: $ st-> size" in perl?

I would like to know how to print the value of a member sizefrom an object File::statin double quotes. I can print "File size: " . $st->sizeget what I want:

File Size: 4003856350

But if I print "File size: $st->size", I get:

File size: File :: stat = ARRAY (0x15bb4d8) → size

I searched for terms such as "perl print dereference", "perl print object member", "perl print arrow operator" and many other combinations. I tried using $st->{size}, but I was told that $ st is not a hash. I tried $st->[size]because the original error seemed to point to an array, but they told me:

The value of the "size" of the bar is not allowed when using "strict subnets"

There seems to be a way for this to work in quotation marks, and if so, then I really would like to know what it is. I know that I can use printffor this without concatenation, but after this much searching, trial and error, my curiosity is aroused.

Code example:

#!/usr/bin/perl -w
use strict;
use utf8;
use File::stat;

my $st = stat("/path/to/file");
print "File size: $st->size\n";
print "File size: " . $st->size . "\n";
printf "File size: %d\n", $st->size;
+4
source share
3 answers

You cannot put Perl code in double quotes and expect it to execute unless you include it in some way eval EXPR.

my $x = 4;
my $y = 5;
print "Sum: $x + $y\n";   # Prints Sum: 4 + 5, not Sum: 9.

Next job:

print "File size: " . $st->size . "\n";

print "File size: ".( $st->size )."\n";

say "File size: " . $st->size;

printf "File size: %s\n", $st->size;

Template->new()->process(\"File size: [% st.size %]\n", { st => $st });

print "File size: ${\( $st->size )}\n";

, . , , , ( ), . . , , - , .

+4

@ikegami , . , , , , . -> Perl :

  • $hash ref, $hash->{key} $hash->{'key'} key.
  • $array ref, $array->[N] N.
  • My::Module , My::Module->member member . , , .
  • , $object , $object->member member, .

, $st->size size File::stat. size - (), $st, . , :

print "File size: $st->size\n";

$st, File::stat=ARRAY(0x15bb4d8) ( ref ), ->size\n. , $st->size(), .

, , doble, :

print 'File size: ' . $st->size . "\n";
0

use the baby basket

Discovered by Larry Wall, 1994. (Alternative pseudonyms: shopping trolls, strollers, tortoises)

It does its job in almost any situation when you need to print the result of a method:

print "File size: @{[ $st->size ]}\n"

see: Perl Secrets

0
source

Source: https://habr.com/ru/post/1691720/


All Articles