Questions tagged [tie]

Perl command to hide an object class in a simple variable.

Perl command to hide an object class in a simple variable. See also: http://perldoc.perl.org/functions/tie.html.

86 questions
28
votes
2 answers

Using std::tie as a range for loop target

I want to do something like the following: //std::vector> someInitializingFunction(); { TypeA a; TypeB b; for (std::tie(a, b) : someInitializingFunction()) { // do stuff; } } However, this is not valid code…
OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
21
votes
5 answers

How can I hook into Perl's print?

Here's a scenario. You have a large amount of legacy scripts, all using a common library. Said scripts use the 'print' statement for diagnostic output. No changes are allowed to the scripts - they range far and wide, have their approvals, and…
Robert P
  • 15,707
  • 10
  • 68
  • 112
15
votes
1 answer

How to make a tuple of const references?

Say there are two functions: void ff( const std::tuple ) { } template < typename TT > void gg( const std::tuple ) { } and calls to these functions: int xx = 0; ff( std::tie( xx ) ); // passes gg( std::tie( xx ) ); // FAILS…
Vahagn
  • 4,670
  • 9
  • 43
  • 72
9
votes
1 answer

Unpacking nested tuples in C++

std::tie provides a convenient way to unpack the contents of a tuple in C++ into separately defined variables, like the sample below illustrates int a, b, c, d, e, f; auto tup1 = std::make_tuple(1, 2, 3); std::tie(a, b, c) = tup1; However, if we…
D R
  • 21,936
  • 38
  • 112
  • 149
8
votes
2 answers

In Perl, is there any way to tie a stash?

Similar to the way AUTOLOAD can be used to define subroutines on demand, I am wondering if there is a way to tie a package's stash so that I can intercept access to variables in that package. I've tried various permutations of the following idea,…
Eric Strom
  • 39,821
  • 2
  • 80
  • 152
8
votes
4 answers

Update the rank in a MySQL Table

I have the following table structure for a table Player Table Player { Long playerID; Long points; Long rank; } Assuming that the playerID and the points have valid values, can I update the rank for all the players based on the number of…
smahesh
  • 663
  • 1
  • 11
  • 21
7
votes
1 answer

Different result when evaluating Readonly variable twice

I noticed that with variables declared with the Readonly module, evaluating a variable multiple times can yield different results. >perl -Mbigint -MReadonly -wE "Readonly my $V => 1; foreach (1..2) { say 0 + '1000000000000001' * $V…
gatinueta
  • 393
  • 3
  • 13
7
votes
2 answers

Why does Tie::File add a line if a file is sorted?

I have this little perl script that is supposed to sort a file: #!/usr/bin/perl use strict; use warnings; use Tie::File; tie my @lines, 'Tie::File', 'fileToBeSorted.txt' or die $!; printf "line count before: %d\n", scalar @lines; @lines= sort…
René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293
7
votes
1 answer

Why is my Perl program failing with Tie::File and Unicode/UTF-8 encoding?

I am working on a project which deals with data in foreign languages. My Perl scripts were running fine. I then wanted to use Tie::File, since this is a neat concept (and saves time and coding). It seems that Tie:File is failing under…
Helen Craigman
  • 1,443
  • 3
  • 16
  • 25
6
votes
4 answers

How can I extract hash values into an array in their insertion order?

Given a hash in Perl (any hash), how can I extract the values from that hash, in the order which they were added and put them in an array? Example: my %given = ( foo => '10', bar => '20', baz => '15' ); I want to get the following result: my…
Tom
  • 6,991
  • 13
  • 60
  • 78
6
votes
1 answer

Does GCC optimize std::tie used only for readability?

Suppose I have a std::tuple: std::tuple t = {1,2,3,4}; and I want to use std::tie just for readability purpose like that: int a, b, c, d; // in real context these names would be meaningful std::tie(a, b, c, d) = t; vs. just using…
syntagma
  • 23,346
  • 16
  • 78
  • 134
5
votes
5 answers

Deferring code on scope change in Perl

I often find it useful to be able to schedule code to be executed upon leaving the current scope. In my previous life in TCL, a friend created a function we called defer. It enabled code like: set fp [open "x"] defer("close $fp"); which was…
mmccoo
  • 8,386
  • 5
  • 41
  • 60
5
votes
2 answers

How can I call methods on a tied variable?

I've just started to learn about tie. I have a class named Link which I would like to do the following thing: if fetched, return the link's address if stored, store the new address be able to call methods on it So far, my code is : package…
Geo
  • 93,257
  • 117
  • 344
  • 520
5
votes
1 answer

How to tie every variable in perl script?

I want to see every place when variable in perl script is created/accessed/destroyed It is easily reachable using tie or Variable::Magic But how to apply this magic automatically when variable is created?
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
5
votes
3 answers

Strange behavior of a tied hash in perl, when asking for an arrayref

I was trying to tie an hash (or hashref) in order of tracking variable usages. Everything is working for simple cases, but when I tried to use my module on some real code I had this error: hash- or arrayref expected (not a simple scalar, use…
kanly
  • 139
  • 9
1
2 3 4 5 6